import { createWebHistory, createRouter } from "vue-router";
import { microAppBaseRouter, microAppForceSendData } from "../microApp/utils";

const Layout = () => import(/** layout */ "@/layout");
const TemplateList = () => import(/** layout */ "@/views/templateList");
const NewTemplateList = () => import(/** layout */ "@/views/newTemplateList");
const ContractingParty = () => import(/** layout */ "@/views/contractingParty");
const ErrorPage = () => import(/** error */ "@/views/error/404.vue");

// 公共路由
export const constantRoutes = [
  {
    path: "/:pathMatch(.*)*",
    component: ErrorPage,
    hidden: true
  },
  {
    path: "/",
    component: Layout,
    redirect: "/template-list",
    children: [
      {
        path: "/template-list",
        component: TemplateList,
        name: "TemplateList",
        meta: { title: "合同模板" }
      },
      {
        path: "/new-template",
        component: NewTemplateList,
        name: "NewTemplateList",
        meta: { title: "合同模板" }
      },
      {
        path: "/contracting-party",
        component: ContractingParty,
        name: "ContractingParty",
        meta: { title: "签约主体" }
      }
    ]
  }
];

const router = createRouter({
  history: createWebHistory(microAppBaseRouter()),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  }
});

router.beforeEach(async (to, from, next) => {
  microAppForceSendData(to);
  next();
});

export default router;
